<?php

namespace Pz\BaseBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * @Route("/install")
 */
class InstallController extends Controller
{
  /**
   * @Route("/login", name="_install_login")
   * @Template()
   */
  public function loginAction(Request $request)
  {
    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
      $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } else {
      $error = $request->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
    }

    return array(
      'last_username' => $request->getSession()->get(SecurityContext::LAST_USERNAME),
      'error'         => $error
    );
  }

  /**
   * @Route("/login_check", name="_install_security_check")
   */
  public function securityCheckAction()
  {
    // The security layer will intercept this request
  }

  /**
   * @Route("/logout", name="_install_logout")
   */
  public function logoutAction()
  {
    // The security layer will intercept this request
  }

  /**
   * @Route("/", defaults={"param"="{}"}),
   * @Route("/{param}", name="_install_index")
   * @Template()
   */
  public function indexAction($param)
  {
    $param = urldecode($param);
    $param = json_decode($param);
    $param->m = isset($param->m) ? $param->m : 'Systems';
    $param->p = isset($param->p) ? $param->p : 1;
    $param->o = isset($param->o) ? $param->o : 'id';
    $param->q = isset($param->q) ? $param->q : 'ASC';
    $param->data = isset($param->data) ? $param->data : array();
    return array('param' => (array)$param);
  }
}
